<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Group\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Common\Models\User;
use IKPHP\Apps\Group\Models\Group;
use IKPHP\Common\Models\Comment;
use IKPHP\Common\Models\Recommend;
use IKPHP\Common\Models\LikeModel;

class GroupTopics extends BaseModel {
	
	public $topicid;
	public $typeid;
	public $groupid;
	public $userid;
	public $title;	
	public $content;
	public $addtime;
	public $uptime;	
	
	public function getSource() {
		return IK . "group_topics";
	}
    /* 用户模型自动完成 */	
    public function beforeCreate()
    {
    	$this->typeid 	= 0;
        $this->count_comment 	= 0;
        $this->count_view	= 0;
        $this->count_like	= 0;
        $this->count_attach	= 0;
        $this->count_recommend	= 0;
        
        
        $this->istop	= 0;
        $this->isaudit	= 0;
        $this->isshow	= 0;
        $this->isreply	= 0;
        $this->isphoto	= 0;
        $this->isattach	= 0;
        $this->isnotice = 0;
        $this->isdigest	= 0;
        $this->isvideo	= 0;

        $this->addtime	= time();
        $this->uptime	= time();
    }
    //更新后重新计算帖子
    public function afterUpdate(){ 
    	//计算全部帖子
    	$countTopic = self::count("groupid='$this->groupid' AND title<>'0'");
    	//计算今天发布的
		$today_start = strtotime ( date ( 'Y-m-d 00:00:00' ) );
		$today_end = strtotime ( date ( 'Y-m-d 23:59:59' ) );
		$where = "groupid = '$this->groupid' AND title<>'0' AND addtime > $today_start";
    	$countTopicToday = self::count($where);
        //更新
        $group = new Group();
		$strMod = $group->findFirst("groupid = '$this->groupid'"); 
		$strMod->count_topic = $countTopic;
		$strMod->count_topic_today = $countTopicToday;
		$strMod->save();
	
    }
	//删除后跟新其他表
    public function afterDelete()
    {
        //更新模型记录集
        $group = new Group();
		$strMod = $group->findFirst("groupid = '$this->groupid'"); 
		$strMod->count_topic = $strMod->count_topic - 1;
		$strMod->save();
		//更新其他表 推荐 喜欢 评论
		$commont = new Comment();
		$commont->find("typeid = '$this->topicid' AND type='GroupTopics' ")->delete(); 
		//更新其他表 推荐 喜欢 评论
		$recom = new Recommend();
		$recom->find("typeid = '$this->topicid' AND type='GroupTopics' ")->delete(); 
		//更新其他表 推荐 喜欢 评论
		$like = new LikeModel();
		$like->find("typeid = '$this->topicid' AND type='GroupTopics' ")->delete(); 				
    }        	
	/**
	 * 最新话题
	 * @param int $groupid 组id
	 * @param int $limit 查询条数
	 * **/
	public static function getNewTopic($groupid, $limit = ''){
		$where = array (
				'conditions'=>"groupid = '$groupid' AND isshow = 0 ",
				'order'=> 'istop desc,uptime desc',
		); 		
		!empty($limit) && $where['limit'] = $limit;
		$results = self::find($where);
		$result = array();
		if(count($results)>0){
			foreach($results->toArray() as $key=>$item){
				$result[$key] = $item;
				$result[$key]['user'] = User::getOneUser($item['userid']);
			}
		}
		return $result;	
	}
	/**
	 * 更多回应、喜欢和推荐时，最受大家喜爱的话题
	 * @param int $groupid 组id
	 * @param int $limit 查询条数
	 * **/
	public static function getEssenceTopics($groupid, $limit = ''){
		$where['conditions'] = "groupid = '$groupid' AND isshow = 0 ";
		$where['order'] = 'count_comment DESC, count_like DESC, count_recommend DESC';				
		!empty($limit) && $where['limit'] = $limit;
		
		$results = self::find($where); 
		$result = array();
		if(count($results)>0){
			foreach($results->toArray() as $key=>$item){
				$result[$key] = $item;
				$result[$key]['user'] = User::getOneUser($item['userid']);
			}
		}
		
		return $result;	
	}	
	
	/**
	 * 获取小组话题 根据 $strgroupid
	 * @param int $groupid 组id
	 **/	
	public static function getTopics($strgroupid,$limit = ''){
		$where['conditions'] = "groupid IN ($strgroupid) AND isshow=0  AND isaudit=0 ";
		$where['order'] = 'uptime desc';
		!empty($limit) && $where['limit'] = $limit;
		$result = array();
		$result = self::find($where); 
		if(count($result)>0){
			return $result->toArray();
		}
		return $result;	
	}
	/**
	 * 根据话题ID获取一条话题
	 * @param int $topicid id
	 **/	
	public static function getOneTopic($topicid) {
		$where['conditions'] = " topicid = '$topicid' AND title <>'0' AND groupid<>'0'";
		$strTopic = array();
		$result = self::findFirst($where); 
		if($result){			
			$strTopic = $result->toArray();
			$content = $strTopic['content'];
			$strTopic['content'] = ikhtml('topic', $topicid, $content,1);
			$strTopic['content_photo'] = ikhtml_img('topic', $topicid, $content);
			if(!empty($strTopic['content_photo'])){
				$strTopic['headimg'] = $strTopic['content_photo'][0]['simg'];			
			}
		}
		//var_dump($strTopic['headimg']);die;
		return $strTopic;
	}
	/**
	 * 获取某用户发表的话题
	 * @param int $userid 
	 **/	
	public static function getPublishTopics($userid,$limit = ''){
		$where['conditions'] = "userid = '$userid' AND isshow=0  AND isaudit=0 AND title<>'0'";
		$where['order'] = 'uptime desc';
		!empty($limit) && $where['limit'] = $limit;
		$result = array();
		$result = self::find($where);  
		if(count($result)>0){
			$result = $result->toArray();
			$result[0]['number'] = self::count($where['conditions']);
			return $result;
		}
		
		return $result;	
	}
	/**
	 * 获取某用户回应的话题
	 * @param int $userid 
	 **/	
	public static function getReplyTopics($userid,$limit = ''){
		$arrResult = array();
		$where['columns']  = 'DISTINCT typeid as typeid';
		$where['conditions'] = "userid = '$userid' AND type='GroupTopics'";
		$where['order'] = 'addtime desc';
		!empty($limit)&&$where['limit'] = $limit;
		
		$total = Comment::count(array('distinct'=>'typeid','conditions'=>$where['conditions']));
		
		$res = Comment::find($where); 

		if(count($res)>0){ 
				
			$instr = '';
			foreach ($res->toArray() as $ki=>$item)
			{
				if($ki==0){
					$instr .= $item['typeid'];
				}else{
					$instr .= ','.$item['typeid'];
				}
			}
			
			$map['conditions'] = "topicid IN ($instr) AND isshow=0  AND isaudit=0 AND title<>'0'";
			$map['order'] = 'uptime DESC';
			
			$result = self::find($map); 
			if($result){
				$result = $result->toArray();
				$arrResult = $result;
				$arrResult[0]['number'] = $total;
			}
			
		}
		
		return $arrResult;	
	}
	/**
	 * 获取某用户喜欢的话题
	 * @param int $userid 
	 **/	
	public static function getLikeTopics($userid,$limit = ''){
		$arrResult = array();
		$where['columns']  = 'typeid,addtime';
		$where['conditions'] = "userid = '$userid' AND type='GroupTopics'";
		$where['order'] = 'addtime desc';
		!empty($limit)&&$where['limit'] = $limit;
		
		$total = LikeModel::count(array('distinct'=>'typeid','conditions'=>$where['conditions']));
		
		$res = LikeModel::find($where); 
		if(count($res)>0){ 			
			foreach($res as $key=>$val){
				$map['conditions'] = "topicid = '$val->typeid' AND isshow=0  AND isaudit=0 AND title<>'0'";
				$result = self::findFirst($map);
				if($result){
					$result = $result->toArray();
					$arrResult[$key] = $result;
					$arrResult[$key]['uptime'] = $val->addtime;
					$arrResult[$key]['number'] = $total;
				}
			}
		}
		return $arrResult;	
	}
	/**
	 * 获取某用户推荐的话题
	 * @param int $userid 
	 **/	
	public static function getRecomTopics($userid,$limit = ''){
		$arrResult = array();
		$where['columns'] = 'typeid,addtime';
		$where['conditions'] = "userid = '$userid' AND type='GroupTopics'";
		$where['order'] = 'addtime desc';
		!empty($limit)&&$where['limit'] = $limit;
		
		$total = LikeModel::count(array('distinct'=>'typeid','conditions'=>$where['conditions']));
					
		$res = Recommend::find($where);
			
		if(count($res)>0){ 			
			foreach($res as $key=>$val){
				$map['conditions'] = "topicid = '$val->typeid' AND isshow=0  AND isaudit=0 AND title<>'0'";
				$result = self::findFirst($map);
				if($result){
					$result = $result->toArray();
					$arrResult[$key] = $result;
					$arrResult[$key]['uptime'] = $val->addtime;
					$arrResult[$key]['number'] = $total;
				}
			}
		}
		
		return $arrResult;	
	}
	/**
	 * 获取全站精选的的话题 推荐最多；评论最多；喜欢最多；置顶；精华；浏览多
	 * @param array $limit 
	 **/	
	public static function getAllEssence($limit = ''){
		$arrResult = array();
		$where['conditions'] = "isshow=0  AND isaudit=0 AND title<>'0'";
		$where['order'] = 'uptime DESC,isphoto DESC,count_comment DESC,count_like DESC,count_recommend DESC,isdigest DESC,count_view DESC';
		!empty($limit)&&$where['limit'] = $limit;
		
		$res = self::find($where);
		if(count($res)>0){
			foreach ($res->toArray() as $key=>$item){
				$map = array('columns'=>'groupid,groupname,count_user','conditions'=>"groupid=".$item['groupid']);
				$arrResult[$key] = $item;
				if($item['isphoto']==1){
					$arrResult[$key]['content_photo'] = ikhtml_img('topic', $item['topicid'], $item['content']);
					$arrResult[$key]['headimg'] = $arrResult[$key]['content_photo'][0]['simg'];	
					
				}else{
					$arrResult[$key]['content_photo'] = '';
				}	
				$group = Group::findFirst($map);
				$arrResult[$key]['groupname'] = $group->groupname;
				$arrResult[$key]['count_user'] = $group->count_user;
			}
		}
		
		return $arrResult;	
	}
	public static  function countAllEssence(){
		$count = self::count(array("isshow=0  AND isaudit=0 AND title<>'0'"));
		return $count;
	}	
	/**
	 * 根据类别名称搜索获取话题
	 * @param array $limit 
	 **/	
	public static function getTopicsByCateName($conditions,$limit = ''){
		$arrResult = array();
		$where['conditions'] = $conditions;
		$where['order'] = 'uptime DESC,isphoto DESC,count_comment DESC,count_like DESC,count_recommend DESC,isdigest DESC,count_view DESC';
		!empty($limit)&&$where['limit'] = $limit;
		
		$res = self::find($where);
		if(count($res)>0){
			foreach ($res->toArray() as $key=>$item){
				$map = array('columns'=>'groupid,groupname,count_user','conditions'=>"groupid=".$item['groupid']);
				$arrResult[$key] = $item;
				if($item['isphoto']==1){
					$arrResult[$key]['content_photo'] = ikhtml_img('topic', $item['topicid'], $item['content']);
					$arrResult[$key]['headimg'] = $arrResult[$key]['content_photo'][0]['simg'];	
					
				}else{
					$arrResult[$key]['content_photo'] = '';
				}	
				$group = Group::findFirst($map);
				$arrResult[$key]['groupname'] = $group->groupname;
				$arrResult[$key]['count_user'] = $group->count_user;
			}
		}
		
		return $arrResult;	
	}							
}
